package jd;

import java.util.List;
import jd.Date;
import jd.Journal;
import jd.Person;
import jd.PublicType;
import jd.Grant;
import jd.ArticleId;
import jd.Identifier;

/*
	CREATE TABLE article (
		id      INTEGER PRIMARY KEY,
		title   VARCHAR,
		pub_model       VARCHAR,
		date_created    DATE,
		date_completed  DATE,
		journal_id      VARCHAR,

		CONSTRAINT fk_journal
			FOREIGN KEY(journal_id)
				REFERENCES journal(id)
	);
*/

/*
	CREATE TABLE reference (
		article_id	INTEGER,
		ref_id		INTEGER,
		PRIMARY KEY(ref_id, article_id)
	);
*/

public class Article {
	public int id;
	public String title;
	public String pub_model;
	public Date date_created;
	public Date date_completed;
	public Journal journal;
	public List<Person> author;
	public List<PublicType> publication_types;
	public List<Grant> grant;
	public List<ArticleId> article_ids;
	public int references[];

	void out_author(Person p) {
		//把这个记录插入author表，如果冲突，就不插入
		System.out.printf("INSERT INTO author (collective_name, " + 
			"last_name, fore_name, initials, affiliation) " + 
			"VALUES (%s) ON CONFLICT DO NOTHING;\n", p.to_str());

		System.out.printf("INSERT INTO author_rel (article_id, author_id) " +
			"(SELECT '%d', id FROM author WHERE %s) ON CONFLICT DO NOTHING;\n", id,
				p.to_cond());
	}

	void out_pub_type(PublicType p) {
		System.out.printf("INSERT INTO pub_type (pid, name) VALUES " +
			"(%s) ON CONFLICT DO NOTHING;\n", p.to_str());

		System.out.printf("INSERT INTO pub_type_rel (article_id, pub_type_id) " +
			"SELECT '%d', id FROM pub_type WHERE %s;\n", 
			id, p.to_cond());
	}

	void out_grant(Grant p) {
		System.out.printf("INSERT INTO _grant (grant_id, acronym, country, agency) VALUES " +
			"(%s) ON CONFLICT DO NOTHING;\n", p.to_str());

		System.out.printf("INSERT INTO grant_rel (article_id, _grant_id) " +
			"(SELECT '%d', id FROM _grant WHERE %s) ON CONFLICT DO NOTHING;\n", 
			id, p.to_cond());
	}

	void out_ref(int ref) {
		System.out.printf("INSERT INTO reference (article_id, ref_id) VALUES " +
			"('%d', '%d') ON CONFLICT DO NOTHING;\n", id, ref);
	}

	void out_article_ids(ArticleId p) {
		System.out.printf("INSERT INTO article_id (ty, aid) VALUES " +
			"(%s) ON CONFLICT DO NOTHING;\n", p.to_str());

		System.out.printf("INSERT INTO article_id_rel (article_id, aid_id) " +
			"(SELECT '%d', id FROM article_id WHERE %s) ON CONFLICT DO NOTHING;\n", 
			id, p.to_cond());
	}

	public void out_sql(Identifier gid) {
		System.out.printf("\n-- Begin of Article %d --\n", id);
		journal.out_sql();
		System.out.printf("INSERT INTO Article (id, title, pub_model, " +
			"journal_id, date_created, date_completed)" + 
			"VALUES ('%d', '%s', '%s', '%s', '%s', '%s');\n",
			id, title.replaceAll("'", "''"),
			pub_model.replaceAll("'", "''"),
			journal.id.replace("'", "''"),
			date_created.to_str(), date_completed.to_str());
		if (author == null)
			return;
		for (Person p : author) {
			out_author(p);
		}

		for (PublicType p : publication_types)
			out_pub_type(p);

		if (grant != null) {
			for (Grant p : grant)
				out_grant(p);
		}

		if (article_ids != null) {
			for (ArticleId p : article_ids)
				out_article_ids(p);
		}

		if (references != null) {
			for (int ref : references)
				out_ref(ref);
		}

		System.out.printf("\n-- End of Article %d --\n", id);
	}
}

